package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.OrderPractise;
import com.zut.domain.Practise;
import com.zut.service.OrderPractiseService;
import com.zut.service.PractiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/practise")
public class PractiseController {

    @Autowired
    private PractiseService practiseService;

    @Autowired
    private OrderPractiseService orderPractiseService;

    /*得到所有练车信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Practise> practises = practiseService.getAll(pageNum, pageSize);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }

    /*得到科目二练车信息*/
    @GetMapping("/2/{pageNum}/{pageSize}")
    public Result getTwo(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Practise> practises = practiseService.getTwo(pageNum, pageSize);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }

    /*得到科目三练车信息*/
    @GetMapping("/3/{pageNum}/{pageSize}")
    public Result getThree(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Practise> practises = practiseService.getThree(pageNum, pageSize);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }

    /*创建练车信息*/
    @PostMapping
    public Result insert(@RequestBody Practise practise) {
        boolean flag = practiseService.insert(practise);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据id删除练车信息*/
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        boolean flag = practiseService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改练车信息*/
    @PutMapping
    public Result Update(@RequestBody Practise practise) {
        boolean flag = practiseService.update(practise);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*得到所有练车场地*/
    @GetMapping("/place/{pageNum}/{pageSize}")
    public Result getPlace(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<String> practises = practiseService.getPlace(pageNum, pageSize);
        Integer code = practises != null ? Code.GET_OK : Code.GET_ERR;
        String msg = practises != null ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }

    /*根据练车场地得到练车信息*/
    @GetMapping("/place/{pageNum}/{pageSize}/{place}")
    public Result getByPlace(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String place) {
        PageInfo<Map<String, String>> practises = orderPractiseService.reservedMsg(pageNum, pageSize, place);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }
}
